package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class EncodingFilter implements Filter {
    protected FilterConfig filterconfig;
    private String targetEncoding = "UTF-8";

    public void init(FilterConfig config) throws ServletException {
        this.filterconfig = config;
        this.targetEncoding = config.getInitParameter("encoding");
        
    }

    public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain) throws IOException, ServletException {
    	 try {
             request.setCharacterEncoding("UTF-8");
             response.setCharacterEncoding("UTF-8");
             HttpServletRequest req = (HttpServletRequest) request;
             HttpServletResponse res = (HttpServletResponse)response;
             String path = ((HttpServletRequest)request).getServletPath();
             HttpSession session = req.getSession();
             //过滤用户中心是否再次登陆
             if(path.indexOf("touser.")>0||path.indexOf("toformula.")>0||path.indexOf("tocostdia.")>0
            		 ||path.indexOf("tocenorderlist.")>0||path.indexOf("tocenorderdetail.")>0||path.indexOf("tosales.")>0){
            	 if(session.getAttribute("USER_CENTER")==null){
            		 res.sendRedirect(req.getContextPath() + "/tologincenter.html");
                	 return;
            	 }
             }
             
             //不过滤公开页
             if(path.indexOf("css/")>0||path.indexOf("flash/")>0||path.indexOf("images/")>0||path.indexOf("js/")>0||path.indexOf("extJs/")>0
            		 ||path.indexOf("welcome")>0||path.indexOf("login")>0||path.indexOf("toindex.")>0||path.indexOf("randCodeImage.jsp")>0
            		 || path.indexOf("backlogin.jsp")>0||path.indexOf("Kaptcha")>0||path.indexOf("kaptcha")>0||path.indexOf("backLogin.action")>0){
 				filterChain.doFilter(request, response);
 				return;
             }
             //过滤用户是否登陆
             if(session.getAttribute("USER_ID") == null){
            	 res.sendRedirect(req.getContextPath() + "/tologin.html");
            	 return;
             }
             //过滤后台用户是否合法
             if((!("E".equals(session.getAttribute("USER_TYPE"))||"A".equals(session.getAttribute("USER_TYPE"))))&&path.indexOf("back/")>0){
            	 res.sendRedirect(req.getContextPath() + "/tologin.html");
            	 return;
             }
            
             filterChain.doFilter(request, response);
             
         } catch (Exception iox) {
        	 filterconfig.getServletContext().log(iox.getMessage());
         }
      
    }
   
    public void setFilterConfig(final FilterConfig filterConfig){
        this.filterconfig=filterConfig;
    }

    public void destroy() {
        this.filterconfig = null;

    }

}
